package cn.ycc1.boot3mvc.controller;

import cn.ycc1.boot3mvc.util.SHA256Util;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author ycc
 * @date 2023/11/18
 */
@Slf4j
@RestController
@Tag(name = "测试")
public class TestController {
    private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @GetMapping("/test2")
    @Operation(summary = "测试sha256")
    public String test2() {
        String text = "需要加密的文本";
        String sha256Hash = SHA256Util.getSHA256Hash(text);
        System.out.println("SHA-256哈希值： " + sha256Hash);
        return sha256Hash;
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口")
    public String test() {
        String pwd = passwordEncoder.encode("123456");
        System.out.println(pwd);
        System.out.println(passwordEncoder.matches( "123456", pwd));
        System.out.println(passwordEncoder.matches("12345", pwd));
        return "test";
    }

    @GetMapping("/pdf2Img")
    @Operation(summary = "测试pdf转图片")
    public String pdf2Img() {
        String inputPdfPath = "C:\\home\\test\\alibaba-manual.pdf";
        String outputImagePath = "C:\\home\\test\\alibaba-manual.jpg";
        try {
            convertPdfToImage(inputPdfPath, outputImagePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "test";
    }

    public static void convertPdfToImage(String inputPdfPath, String outputImagePath) throws IOException {
        // 加载PDF文档
        PDDocument document = PDDocument.load(new File(inputPdfPath));

        // 创建PDF渲染器
        PDFRenderer renderer = new PDFRenderer(document);

        // 获取PDF的总页数
        int totalPages = document.getNumberOfPages();

        // 创建一个空白的图片，用于绘制每一页的内容
        BufferedImage combinedImage = null;

        totalPages = 3;
        // 遍历每一页
        for (int i = 0; i < totalPages; i++) {
            // 将当前页转换为图片
            BufferedImage image = renderer.renderImageWithDPI(i, 300);

            // 如果是第一页，则直接使用该图片作为基础图片
            if (i == 0) {
                combinedImage = new BufferedImage(image.getWidth(), image.getHeight() * totalPages, BufferedImage.TYPE_INT_RGB);
            }

            // 将当前页的图片绘制到基础图片上
            combinedImage.getGraphics().drawImage(image, 0, i * image.getHeight(), null);
        }

        // 保存合成后的图片到指定路径
        ImageIO.write(combinedImage, "PNG", new File(outputImagePath));

        // 关闭文档
        document.close();
    }

    public static void convertPdfToImage2(String inputPdfPath, String outputImagePath) throws IOException {
        // 加载PDF文档
        PDDocument document = PDDocument.load(new File(inputPdfPath));
        // 创建PDF渲染器
        PDFRenderer renderer = new PDFRenderer(document);
        // 获取PDF的总页数
        int totalPages = document.getNumberOfPages();

        // 遍历每一页
        for (int i = 0; i < totalPages; i++) {
            // 将当前页转换为图片
            BufferedImage image = renderer.renderImageWithDPI(i, 300);

            // 构建输出文件名
            String outputFileName = "page" + (i + 1) + ".jpg";

            // 保存图片到指定路径
            ImageIO.write(image, "JPG", new File(outputFileName));
        }

        // 关闭文档
        document.close();
    }

    public static void convertPdfToImage3(String inputPdfPath, String outputImagePath) throws IOException {
        // 加载PDF文档
        PDDocument document = PDDocument.load(new File(inputPdfPath));
        // 创建PDF渲染器
        PDFRenderer renderer = new PDFRenderer(document);
        // 将第一页转换为图片（这里可以根据需要修改页码）
        BufferedImage image = renderer.renderImageWithDPI(0, 300);
        // 保存图片到指定路径
        ImageIO.write(image, "PNG", new File(outputImagePath));
        // 关闭文档
        document.close();
    }

}
